package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyJiaYi;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 *  安心分期-天津佳亿
 *
 *  @author WJB
 */
@ApiPlan(productId = "34", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5JiaYi implements ApiOrgPhoneMd5ConvertHandler {

    private static final String CHANNEL_CODE = "13";
    private static final String CHANNEL_KEY = "AnXinFen20250318";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        String key = "MCXBIjANke";

        String time = ClassConvertUtils.toStr(System.currentTimeMillis());
        String aesKey = key + time;
        AES aes = AES.createEcb(aesKey.substring(0, 16).getBytes());

        Map<Object, Object> signs = MapUtils.newHashMap(newMap -> {
            newMap.put("mobileMd5", apiDataExchangeParameter.getPhoneMd5());
            newMap.put("city", apiDataExchangeParameter.getCity());
        });

        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("timestamp", time);
            newMap.put("signs", aes.encryptToHex(JsonUtils.toJson(signs)));
        });
        String json = JsonUtils.toJson(map);
        return json;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getString("result"), "200")) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public String apply(ApiApplyParameter apiApplyParameter) {
        //  拓展判断信息
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);

        OrgApplyJiaYi data = new OrgApplyJiaYi();
        //  渠道标识
        data.setChannel_code(CHANNEL_CODE);
        //  key
        data.setChannel_secret_key(CHANNEL_KEY);
        //  姓名
        data.setName(apiApplyParameter.getUserName());

        AES aes = AES.createEcb(CHANNEL_KEY.substring(0, 16).getBytes());
        //  手机号
        if (ValidParam.isNotEmpty(apiApplyParameter.getPhone())) {
            data.setPhone(aes.encryptToHex(apiApplyParameter.getPhone()));
        }
        //  性别
        Integer sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", 0)
                .when(apiApplyParameter.getSex(), "1", 1)
                .end();
        data.setSex(sex);
        //  年龄
        data.setAge(apiFieldConverter.getAgeInt());
        //  贷款金额
        data.setQuota(apiFieldConverter.getLoanAmountInt());
        //  房产情况
        if(ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            Integer house = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", 2)
                    .end(1);
            data.setHouse(house);
        }
        //  车产
        if(ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            Integer car = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", 2)
                    .end(1);
            data.setCar(car);
        }
        //  社保
        if(ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
            Integer shebao = ChooseEq.create()
                    .when(apiApplyParameter.getSocialSecurity(), "1", 2)
                    .end(1);
            data.setSocial_security(shebao);
        }
        //  信用卡
        if(ValidParam.isNotEmpty(apiApplyParameter.getCreditLimit())) {
            Integer creditCard = ChooseEq.create()
                    .when(apiApplyParameter.getCreditLimit(), "1", 2)
                    .end(1);
            data.setCredit_card(creditCard);
        }
        //  营业执照
        if(ValidParam.isNotEmpty(apiApplyParameter.getBizLicenseYear())) {
            Integer bizLicenseYear = ChooseEq.create()
                    .when(apiApplyParameter.getBizLicenseYear(), "1", 2)
                    .end(1);
            data.setBusiness_license(bizLicenseYear);
        }
        //  公积金
        if(ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            Integer gongjijin = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", 2)
                    .end(1);
            data.setAccumulation_fund(gongjijin);
        }
        //  保单
        if(ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
            Integer baodanIs = ChooseEq.create()
                    .when(apiApplyParameter.getInsurancePolicy(), "1", 2)
                    .end(1);
            data.setInsurance_policy(baodanIs);
        }
        //  代发
        if(ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getSalaryPayMethod(), "1", 1)
                    .end(2);
            data.setHava_issued(text);
        }
        String json = JsonUtils.toJson(data);
        return json;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
